-- TextStyleM
-- create by zouyb
-- modify by limj Apr/25 2015
-- 文字样式定义

module("TextStyleM", package.seeall);

-- 预定义每行的文字数量
TEXT_WORD_COUNT_PER_LINE    = 19;                               -- 每行19个字

-- 预定义行间距
TEXT_LINE_HIGHT_MIDDLE      = 38;                               -- 公告行间距

-- 预定义字号
TEXT_SIZE_LINE              = 10;                               -- 下划线
TEXT_SIZE_DATE              = 16;                               -- 评论日期
TEXT_SIZE_TINY2             = 18;                               -- 最小号
TEXT_SIZE_TINY              = 20;                               -- 最小号
TEXT_SIZE_MINI              = 22;                               -- 说明文字
TEXT_SIZE_SMALL2            = 24;                               -- 说明文字
TEXT_SIZE_SMALL             = 26;                               -- 说明文字
TEXT_SIZE_MIDDLE            = 30;                               -- 标题框文字，按钮文字
TEXT_SIZE_MIDDLE2           = 36;                               -- 结算界面
TEXT_SIZE_LARGE             = 40;                               -- 魔法卷轴标题
TEXT_SIZE_LARGE2            = 48;                               -- 格子中毒显示
TEXT_SIZE_LARGE3            = 60;                               -- 全清奖励等标题
TEXT_SIZE_HUGE              = 72;                               -- 冈布奥/冈布奥学徒获得、等级提升等显著提示

-- 预定义文本颜色
TEXT_COLOR_DARK_BROWN       = cc.c4b(0x32, 0x23, 0x10, 0xff);   -- 深棕
TEXT_COLOR_YELLOW_BROWN     = cc.c4b(0x73, 0x43, 0x1d, 0xff);   -- 棕黃色(适用于黄色背景的描边)
TEXT_COLOR_DARK_RED         = cc.c4b(0x8d, 0x1d, 0x16, 0xff);   -- 暗红
TEXT_COLOR_BRIGHT_RED       = cc.c4b(0xff, 0x00, 0x30, 0xff);   -- 亮红
TEXT_COLOR_LIGHT_RED        = cc.c4b(0xf8, 0x66, 0x62, 0xff);   -- 浅红
TEXT_COLOR_ORANGE           = cc.c4b(0xf2, 0x9f, 0x55, 0xff);   -- 橙黄
TEXT_COLOR_LIGHT_ORANGE     = cc.c4b(0xf2, 0xc4, 0x40, 0xff);   -- 亮橙
TEXT_COLOR_BROWN_YELLOW     = cc.c4b(0xf3, 0xe1, 0xa3, 0xff);   -- 土黄
TEXT_COLOR_BEIGE            = cc.c4b(0xfe, 0xf2, 0xc8, 0xff);   -- 米黄
TEXT_COLOR_BRIGHT_YELLOW    = cc.c4b(0xfd, 0xe3, 0x5a, 0xff);   -- 亮黄
TEXT_COLOR_LIGHT_YELLOW     = cc.c4b(0xef, 0xea, 0x70, 0xff);   -- 淡黄
TEXT_COLOR_GREEN            = cc.c4b(0x16, 0x8d, 0x3d, 0xff);   -- 绿色
TEXT_COLOR_BRIGHT_GREEN     = cc.c4b(0x38, 0xe3, 0x68, 0xff);   -- 亮绿
TEXT_COLOR_LIGHT_GREEN      = cc.c4b(0x7f, 0xe3, 0x9e, 0xff);   -- 浅绿
TEXT_COLOR_BLUE             = cc.c4b(0x3e, 0x65, 0xc7, 0xff);   -- 蓝色
TEXT_COLOR_LIGHT_BLUE       = cc.c4b(0x9e, 0xd4, 0xec, 0xff);   -- 浅蓝色
TEXT_COLOR_BRIGHT_BLUE      = cc.c4b(0x99, 0xfb, 0xe5, 0xff);   -- 亮蓝色
TEXT_COLOR_SKY_BLUE         = cc.c4b(0x18, 0xac, 0xfc, 0xff);   -- 天蓝色
TEXT_COLOR_DARK_BLUE1       = cc.c4b(0x18, 0x41, 0x57, 0xff);   -- 深蓝色
TEXT_COLOR_DARK_BLUE2       = cc.c4b(0x3c, 0x6b, 0x83, 0xff);   -- 深蓝色
TEXT_COLOR_CREAM            = cc.c4b(0xfc, 0xf4, 0xca, 0xff);   -- 米白色
TEXT_COLOR_WHITE            = cc.c4b(0xff, 0xff, 0xff, 0xff);   -- 白色
TEXT_COLOR_GRAY             = cc.c4b(0x6a, 0x6a, 0x6a, 0xff);   -- 灰色
TEXT_COLOR_GRAY2            = cc.c4b(0xc0, 0xc0, 0xc0, 0xff);   -- 灰色2
TEXT_COLOR_GRAY3            = cc.c4b(0x3c, 0x3c, 0x3c, 0xff);   -- 灰色3
TEXT_COLOR_GOLD             = cc.c4b(0xfd, 0xe6, 0x7b, 0xff);   -- 金色
TEXT_COLOR_CYAN             = cc.c4b(0x52, 0xb1, 0xff, 0xff);   -- 蓝色2
TEXT_COLOR_DARK_BLUE        = cc.c4b(0x10, 0x32, 0x32, 0xff);   -- 暗蓝色
TEXT_COLOR_BLACK            = cc.c4b(0x10, 0x10, 0x10, 0xff);   -- 黑色
TEXT_COLOR_TITLE_BLACK      = cc.c4b(0x1C, 0x1C, 0x1C, 0xff);   -- 标题描边黑色
TEXT_COLOR_GOLD_YELLOW      = cc.c4b(0xd9, 0x8b, 0x37, 0xff);   -- 金黄色
TEXT_COLOR_GOLD_YELLOW2     = cc.c4b(0xe5, 0x9b, 0x2a, 0xff);   -- 金黄色2

-- 预定义系别文本颜色
SKILL_TEXT_COLOR_EARTH      = cc.c4b(0xe3, 0xc9, 0x72, 0xff);   -- 土
SKILL_TEXT_COLOR_WATER      = cc.c4b(0x1a, 0x7e, 0xc0, 0xff);   -- 水
SKILL_TEXT_COLOR_FIRE       = cc.c4b(0xd4, 0x56, 0x4e, 0xff);   -- 火
SKILL_TEXT_COLOR_THUNDER    = cc.c4b(0xa0, 0xea, 0x92, 0xff);   -- 气
SKILL_TEXT_COLOR_LIGHTER    = cc.c4b(0xe6, 0xe0, 0x9b, 0xff);   -- 光
SKILL_TEXT_COLOR_DARK       = cc.c4b(0xca, 0x92, 0xed, 0xff);   -- 暗

-- 预定义描边颜色
TEXT_OUTLINE_COLOR          = cc.c4b(0x32, 0x23, 0x10, 0xff);   -- 深色
TEXT_OUTLINE_COLOR_BRIGHT   = cc.c4b(0xf5, 0xf7, 0xc9, 0xff);   -- 亮色
TEXT_OUTLINE_COLOR_DARK     = cc.c4b(0x10, 0x10, 0x10, 0xff);   -- 黑色
TEXT_OUTLINE_COLOR_BLACK     = cc.c4b(0x00, 0x00, 0x00, 0xff);   -- 黑色

-- 渠道登录按钮专用描边
TEXT_OUTLINE_COLOR_GOOGLE     =  cc.c4b(0x5e, 0x09, 0x0a, 0xff);   -- Google+
TEXT_OUTLINE_COLOR_GUEST      =  cc.c4b(0x29, 0x33, 0x01, 0xff);   -- 游客模式
TEXT_OUTLINE_COLOR_GAMECENTER =  cc.c4b(0x6b, 0x0b, 0x3f, 0xff);   -- Game Center
TEXT_OUTLINE_COLOR_FACEBOOK   =  cc.c4b(0x21, 0x2b, 0x85, 0xff);   -- Facebook

-- 预定义描边宽度
TEXT_OUTLINE_SIZE           = 2;                                 -- 2个像素

-- 变灰的颜色
TEXT_COLOR_BLEND         = cc.c4b(0x67, 0x69, 0x6c, 0xff);       -- 图案变暗的混色
TEXT_COLOR_NORMAL        = cc.c4b(0xff, 0xff, 0xff, 0xff);       -- 白色

-- 预定义阴影偏移
TEXT_SHADOW_OFFSET       = cc.size(0, -2);                        -- 默认偏移1像素

-- 重置文本格式
function resetTextStyle(targetNode)

    if targetNode.getTitleRenderer ~= nil then
        -- 传入的是按钮
        targetNode:getTitleRenderer():setAdditionalKerning(0);
        targetNode:setTitleFontSize(TEXT_SIZE_SMALL);
        targetNode:setTitleColor(TEXT_COLOR_DARK_BROWN);
        targetNode:getTitleRenderer():enableOutline(TEXT_OUTLINE_COLOR, 0);
        return;
    end

    targetNode:setScaleX(1);
    targetNode:setScaleY(1);
    targetNode:setFontSize(TEXT_SIZE_SMALL);
    targetNode:setColor(TEXT_COLOR_WHITE);
    targetNode:setTextColor(TEXT_COLOR_DARK_BROWN);
    targetNode:getVirtualRenderer():setAdditionalKerning(0);
    targetNode:enableOutline(TEXT_OUTLINE_COLOR, 0);
    targetNode:disableEffect();
end

--------------------------------------
-- 设置文本的样式
-- @function [#parent=TextStyleM]
-- @param targetNode        目标文本节点, 需要为cc.Text或者cc.Button类型
-- @param fontStyleSize     字号
-- @param fontStyleColor    颜色
-- @param needOutline       是否描边（默认不描边）
-- @param kerning           缩进（默认为0）
function setTextStyle(targetNode, fontStyleSize, fontStyleColor, needOutline, kerning, smoothScaleSize, noFix, wordCount, lineHeight, needTextShadow, scaleSize)
    -- 文本默认 26 号字、深棕色、无描边
    local oldFontSize = fontStyleSize or TEXT_SIZE_SMALL;
    local fontSize    = oldFontSize;
    local scale       = scaleSize or 1;

    -- 字号整体缩小
    fontSize = oldFontSize * getLocFontSizeMulti(noFix) * scale;

    local color       = fontStyleColor or TEXT_COLOR_DARK_BROWN;
    local outline     = needOutline or false;
    local smoothScale = smoothScaleSize or 1;
    local olColor     = targetNode.olColor or TEXT_OUTLINE_COLOR;
    local olSize      = targetNode.olSize or TEXT_OUTLINE_SIZE * smoothScale * iif(needOutline, 1, 0);

    local fontName    = getLocFontName();

    -- 为了平滑描边，这里先将文本放大，之后再设置比例缩回去
    local oldScaleX = targetNode:getScaleX();
    local oldScaleY = targetNode:getScaleY();

    if not targetNode.initScaleX then
        targetNode.initScaleX = oldScaleX;
        targetNode.initScaleY = oldScaleY;
    end

	if targetNode.getTitleRenderer ~= nil then
		-- 传入的是按钮，需要加描边

        -- 字号增加 6 号
        local fixFontSize = 6;

        -- 设置文本缩进
        local textKerning = kerning or 0;

        -- 按钮文本全部增加一个 -n 的缩进
        textKerning = textKerning + getBtnKerningFix();

        -- 某些语言特殊修正
        textKerning = kerningFix(textKerning);

        -- 按钮文字统一放大 n 个字号
        local fontSizeFix = getBtnFontSizeFix();

        targetNode:setTitleColor(color);
        local ttfConfig  = {}
        ttfConfig.fontFilePath = fontName;
        ttfConfig.fontSize = fontSize + fontSizeFix;
        ttfConfig.outlineSize = olSize;
        targetNode:getTitleRenderer():setOutlineColor(olColor);
        targetNode:getTitleRenderer():setTTFConfig(ttfConfig);
        targetNode:getTitleRenderer():setAdditionalKerning(textKerning + getLocKerning());

        -- 设置文字居中对齐
        targetNode:getTitleRenderer():setHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);

        if needOutline ~= false then
            if getLang() == "en" then
                TextStyleM.setShadowStyle(targetNode, cc.size(-0.3, -2.5));
                TextStyleM.setOutlineStyle(targetNode, 1.6, TEXT_COLOR_DARK_BROWN, true);
            else
                TextStyleM.setShadowStyle(targetNode, cc.size(0, -3));
                TextStyleM.setOutlineStyle(targetNode, 1.6, TEXT_OUTLINE_COLOR, true);
            end
        end

		return;
	end

	-- 传入的是文本框
	-- 设置字体大小/颜色/描边
    targetNode:setTTFConfig(fontName, fontSize * smoothScale, olColor,  olSize);
    targetNode:setColor(TEXT_COLOR_WHITE);
    targetNode:setTextColor(color);

    if needTextShadow then
        if getLang() == "en" then
            TextStyleM.setShadowStyle(targetNode, cc.size(-0.3, -3));
            TextStyleM.setOutlineStyle(targetNode, 1.6, TEXT_COLOR_DARK_BROWN, true);
        else
            TextStyleM.setShadowStyle(targetNode, cc.size(-0.3, -3.3));
            TextStyleM.setOutlineStyle(targetNode, 1.6, TEXT_OUTLINE_COLOR, true);
        end
    end

    -- 设置文本缩进
    local textKerning = kerning or 0;

    -- 根据版本情况，统一增加缩进
    if textKerning == 0 then
        textKerning = textKerning + getLocKerning();
    end

    -- 某些语言特殊修正
    textKerning = kerningFix(textKerning);

    targetNode:getVirtualRenderer():setAdditionalKerning(textKerning * smoothScale);

    -- 设置换行
    local wordCountFix = 1;

    -- 非中文版本的默认每行字数需要根据缩放比例变化
    if getLang() ~= "zh" and getLang() ~= "tw" then
        wordCountFix = getLocFontSizeMulti(noFix) * scale;
    end

    if wordCount == nil then
        targetNode:getVirtualRenderer():setMaxLineWidth(TEXT_WORD_COUNT_PER_LINE / wordCountFix * fontSize + fontSize / 8 + textKerning * (TEXT_WORD_COUNT_PER_LINE / wordCountFix - 1));
    else
        targetNode:getVirtualRenderer():setMaxLineWidth(wordCount * fontSize + fontSize / 8 + textKerning * (wordCount - 1));
    end

    -- 如果有传行距进来，需要设置行高
    if lineHeight then
        targetNode:getVirtualRenderer():setLineHeight(lineHeight);
    end

	targetNode:setScaleX(targetNode.initScaleX / smoothScale);
    targetNode:setScaleY(targetNode.initScaleY / smoothScale);
end

--------------------------------------
-- 设置描边的样式
-- @function [#parent=TextStyleM]
-- @param targetNode        目标文本节点, 需要为cc.Text或者cc.Button类型
-- @param outlineSize       描边宽度，单位为像素（默认 2 像素）
-- @param outlineColor      描边颜色（默认深棕色）
function setOutlineStyle(targetNode, outlineSize, outlineColor, apply)
    local olSize  = outlineSize or TEXT_OUTLINE_SIZE;
    local olColor = outlineColor or TEXT_OUTLINE_COLOR;

    targetNode.olSize = olSize;
    targetNode.olColor = olColor;

    if apply then
        if targetNode.getTitleRenderer ~= nil then
            -- 是按钮
            targetNode:getTitleRenderer():enableOutline(olColor, olSize);
        else
            targetNode:enableOutline(olColor, olSize);
        end
    end
end

-------------------------------------
-- 设置阴影的样式
-- @function [#parent=TextStyleM]
-- @param targetNode        目标文本节点, 需要为cc.Text或者cc.Button类型
-- @param shadowOffset      阴影的偏移
-- @param shadowColor       阴影的颜色
-- @param blurRadius        阴影高斯模糊效果
-- @param needOutline       是否需要描边
function setShadowStyle(targetNode, shadowOffset, shadowColor, blurRadius, needOutline)
    local oldOffset = shadowOffset or TEXT_SHADOW_OFFSET;
    local oldColor = shadowColor or TEXT_OUTLINE_COLOR;
    local oldBlur = blurRadius or 0;
    local oldOutline = needOutline or true;

    if targetNode.getTitleRenderer ~= nil then
        -- 按钮
        if not oldOutline then
            targetNode:getTitleRenderer():disableEffect(cc.LabelEffect.OUTLINE);
        end
        targetNode:getTitleRenderer():enableShadow(oldColor, oldOffset, oldBlur);
    else
        -- 文本
        if not oldOutline then
            targetNode:disableEffect(cc.LabelEffect.OUTLINE);
        end
        targetNode:enableShadow(oldColor, oldOffset, oldBlur);
    end
end

-------------------------------
-- 获取缩放比例
-- 如果单行文本字数超出了限定数量，则需要缩小
-- @param wCharCount        字符数量
-- @param text              文本
function getTextScale(text, limitCharCount, noFix)
    local pureText = extractString(text);
    local strTable = string.explode(pureText, "\n");
    local charCount = limitCharCount or TEXT_WORD_COUNT_PER_LINE * 2;

    local longestStr;
    local finalCount = 0;
    local strCount;

    -- 因为有强制换行的情况，需要根据最长的一行确定缩放比例
    for _, s in pairs(strTable) do
        -- 针对泰语还要单独处理下，泰语有占数量但是不占位置的字符 sigh
        if getLang() == "th" then
            strCount = string.getWCharCountForThai(s);
        else
            strCount = string.getWCharCount(s);
        end
        finalCount = math.max(strCount, finalCount);
    end

    if isFullWidthLang() then
        return math.min(1, (charCount / getLocFontSizeMulti(noFix) / 2) / finalCount);
    else
        return math.min(1, charCount / getLocFontSizeMulti(noFix) / finalCount);
    end
end

-------------------------------
-- 是否白色
function isWhite(color)
    return color.r == 255 and color.g == 255 and color.b == 255;
end

-------------------------------
-- 获取颜色
function getColor(color)
    if color == "red" then
        return TEXT_COLOR_DARK_RED;
    elseif color == "gray" then
        return TEXT_COLOR_GRAY;
    elseif color == "gold" then
        return TEXT_COLOR_GOLD;
    elseif color == "yellow" then
        return TEXT_COLOR_BROWN_YELLOW;
    elseif color == "blue" then
        return TEXT_COLOR_BLUE;
    elseif color == "brown" then
        return TEXT_COLOR_DARK_BROWN;
    elseif color == "green" then
        return TEXT_COLOR_GREEN;
    elseif color == "light_green" then
        return TEXT_COLOR_LIGHT_GREEN;
    elseif color == "light_blue" then
        return TEXT_COLOR_LIGHT_BLUE;
    elseif color == "light_red" then
        return TEXT_COLOR_LIGHT_RED;
    elseif color == "cyan" then
        return TEXT_COLOR_CYAN;
    elseif color == "black" then
        return TEXT_COLOR_BLACK;
    elseif color == "gray2" then
        return TEXT_COLOR_GRAY2;
    elseif color == "orange" then
        return TEXT_COLOR_ORANGE;
    elseif color == "light_orange" then
        return TEXT_COLOR_LIGHT_ORANGE;
    end

    return TEXT_COLOR_WHITE;
end

-- 使用标题模板
function setTitleStyle(label, scale)
    --label.olSize = 3;
    label.olColor = TEXT_COLOR_TITLE_BLACK;
    local textScale = scale or 1;

    -- 设置文本居中对齐
    label:getVirtualRenderer():setHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);
    TextStyleM.setTextStyle(label, TextStyleM.TEXT_SIZE_MIDDLE2, TextStyleM.TEXT_COLOR_ORANGE, true, -2, nil, nil, nil, TextStyleM.TEXT_SIZE_MIDDLE2, true, textScale);
end

-- 需要中文缩进的标题模板
function setTitleStyle2(label, scale)
    local kerning = -4;
    if not isChineseLang() then
        kerning = -1;
    end
    local textScale = scale or 1;

    label.olColor = TEXT_COLOR_TITLE_BLACK;

    -- 设置文本居中对齐
    label:getVirtualRenderer():setHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);
    TextStyleM.setTextStyle(label, TextStyleM.TEXT_SIZE_MIDDLE2, TextStyleM.TEXT_COLOR_ORANGE, true, kerning, nil, nil, nil, TextStyleM.TEXT_SIZE_MIDDLE2, true, textScale);
end

-- 使用副标题模板
function setSubheadStyle(label, scale)
    local kerning = -3;
    if not isChineseLang() then
        kerning = -1;
    end

    local textScale = scale or 1;

    -- 设置文本居中对齐
    label:getVirtualRenderer():setHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);
    TextStyleM.setTextStyle(label, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, kerning, nil, nil, nil, TextStyleM.TEXT_SIZE_MIDDLE, true, textScale);
end

-- 使用艺术字标题目标
function setArtWordTitleStyle(label)
    local kerning = 0;

    local offset = cc.p(0, 0);

    if getLang() == "en" then
        offset = cc.p(20, 0);
    end

    label:removeAllChildren();
    TextStyleM.setTextStyle(label, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_LIGHT_ORANGE, true, kerning, nil, nil, nil, nil, true);
    convertTextToArtWord(label, "images/ui/text_mask/text_mask2.png", nil, nil, nil, cc.p(0, -2), offset);
end

-- 颜色格式转换4B->4F
function changeColor4BTo4F(color4B)
    return cc.c4f(color4B.r/255.0, color4B.g/255.0, color4B.b/255.0, color4B.a/255.0);
end